//
//  ChatBarFaceViewTopView.m
//  keyboard
//
//  Created by 遇水 on 16/3/12.
//  Copyright © 2016年 遇水. All rights reserved.
//

#import "ChatBarFaceViewTopView.h"
#import "UIView+Extension.h"
#import "ChatBarFaceViewTopViewPageView.h"
#import "Utiles.h"
#import "ChatBarFaceViewTopViewConstomPageView.h"

@interface ChatBarFaceViewTopView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView    *scrollView;

@property (nonatomic, strong) UIPageControl    *pageControl;

@end

@implementation ChatBarFaceViewTopView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self initSubview];
    }
    
    return self;
}

-(void)initSubview
{
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.pagingEnabled = YES;
    scrollView.delegate  = self;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.bounces = NO;
    [self addSubview:scrollView];
    self.scrollView = scrollView;
    
    
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    pageControl.userInteractionEnabled = NO;
    pageControl.hidesForSinglePage  = YES;
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor darkGrayColor];
    
    [self addSubview:pageControl];
    self.pageControl = pageControl;
}




-(void)setDataSource:(NSArray *)dataSource
{
    _dataSource = dataSource;
    //获得页数
    NSUInteger count = (dataSource.count + facePageSize - 1) / facePageSize;
    
    // 1.设置页数
    self.pageControl.numberOfPages = count;

    // 2.创建用来显示每一页表情的控件
    for (int i = 0; i<count; i++) {
    
        // 计算这一页的表情范围
        NSRange range;
        range.location = i * facePageSize;
        // left：剩余的表情个数（可以截取的）
        NSUInteger left = dataSource.count - range.location;
        if (left >= facePageSize) { // 这一页足够20个
            range.length = facePageSize;
        } else {
            range.length = left;
        }

        //如果不是说明没有设置恩  所以加载的是普通的，如果是的话说明加载的是自定义的
        if(_isCustomView){
          ChatBarFaceViewTopViewConstomPageView  *pageView = [[ChatBarFaceViewTopViewConstomPageView alloc]init];
            pageView.dataSource = [dataSource subarrayWithRange:range];
            [self.scrollView addSubview:pageView];
        }else{
            
            ChatBarFaceViewTopViewPageView *pageView = [[ChatBarFaceViewTopViewPageView alloc] init];
            pageView.dataSource = [dataSource subarrayWithRange:range];
            [self.scrollView addSubview:pageView];
        }
        
    }
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    self.pageControl.width = self.width;
    self.pageControl.height = 25;
    self.pageControl.x = 0;
    self.pageControl.y = self.height - self.pageControl.height;
    
    
    self.scrollView.width = self.width;
    self.scrollView.height = self.pageControl.y;
    self.scrollView.x = self.scrollView.y = 0;
    
    
    // 3.设置scrollView内部每一页的尺寸
    NSUInteger count = self.scrollView.subviews.count;
    for (int i = 0; i<count; i++) {
        ChatBarFaceViewTopViewPageView *pageView = self.scrollView.subviews[i];
        pageView.height = self.scrollView.height;
        pageView.width = self.scrollView.width;
        pageView.x = pageView.width * i;
        pageView.y = 0;
    }
    
    // 4.设置scrollView的contentSize
    self.scrollView.contentSize = CGSizeMake(count * self.scrollView.width, 0);
    
    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double pageNum = scrollView.contentOffset.x / scrollView.width;
    self.pageControl.currentPage = (int)(pageNum + 0.5);
}


@end
